<template>
  <div class="edit-container">
    <el-form v-loading="formLoading" ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="封面" prop="bannerUrl">
        <div style="display: flex;align-items: center;">
          <el-image v-if="form.bannerUrl" :src="imageUrl+form.bannerUrl" class="img"
                    referrerpolicy="no-referrer"></el-image>
          <el-button type="primary" @click="uploads.show=true" size="mini">选择图片</el-button>
        </div>
      </el-form-item>
      <el-form-item label="标题" prop="name">
        <el-input v-model="form.name" placeholder="标题" maxlength="30"></el-input>
      </el-form-item>
      <el-form-item label="位置" prop="type">
        <el-select v-model="form.type" placeholder="请选择">
          <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="type" v-if="form.type === 4||form.type === 6">
        <el-select v-model="form.classification" placeholder="请选择">
          <el-option
              v-for="item in classList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否跳转" prop="jump">
        <el-select v-model="form.jump" placeholder="请选择" class="col">
          <el-option
              v-for="item in isJump"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商家" prop="jumpPartnerId" v-if="form.jump === 1">
        <el-select v-model="form.jumpPartnerId" clearable filterable placeholder="请选择" style="width: 200px">
          <el-option
              v-for="item in list"
              :key="item.id"
              :label="item.businessName"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.sort" placeholder="请输入排序"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button v-loading="saveLoading" element-loading-background="rgba(0, 0, 0, 0.3)" type="primary"
                   @click="handlerSubmit" v-has-permi="['admin:image:save']">保存
        </el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
    <el-dialog
        :visible.sync="uploads.show"
        title="上传封面"
        :append-to-body="true"
        destroy-on-close
        :close-on-click-modal="false"
        width="1000px">
      <UploadFile
          v-if="uploads.show"
          :multiple="uploads.multiple"
          :module-id="4"
          @getFile="getFile"
          ref="uploads"
      />
    </el-dialog>
  </div>
</template>
<script>
import {detail, save} from "@/api/image";
import {getForm} from "@/utils/utils";
import UploadFile from "@/components/File/UploadFile.vue";
import {sysCategoryList} from "@/api/menu";
import {getOrders} from "@/api/order";

export default {
  components: {UploadFile},
  props: {
    id: {
      default: null
    }
  },
  data() {
    return {
      form: {
        id: null,
        sort: 0,
        bannerUrl: '',
        name: '',
        type: '',
        classification: '',
        jump: '',
        jumpPartnerId: ''
      },
      rules: {
        bannerUrl: [{required: true, message: '请上传图片', trigger: ['blur']}],
        name: [{required: true, message: '请填写标题', trigger: ['blur']}],
        type: [{required: true, message: '请选择类型', trigger: ['blur']}],
        jump: [{required: true, message: '请选择是否可以跳转', trigger: ['blur']}],
      },
      roles: [],
      saveLoading: false,
      formLoading: false,
      uploads: {
        show: false,
        multiple: false,
      },
      imgUploads: {
        show: false,
        multiple: true,
      },
      typeList: [
        {id: 1, name: '首页轮播图'},
        {id: 2, name: '首页广告位'},
        {id: 3, name: '活动轮播图'},
        {id: 4, name: '活动板块图'},
        {id: 5, name: '活动板块图'},
        {id: 6, name: '活动板块详情图'},
      ],
      classList: [{id: 1, name: '免费'}, {id: 5, name: '消费多少送多少'}],
      isJump: [{id: 1, name: '是'}, {id: 2, name: '否'}],
      imageUrl: '',
      list: [],
    }
  },
  mounted() {
    this.imageUrl = process.env.VUE_APP_IMAGE_URL
    this.getBusinessList()
    if (this.id) {
      this.getDetail();
    }
  },
  methods: {
    getBusinessList() {
      getOrders().then(res => {
        this.list = res;
      })
    },
    getFile(fileList) {
      this.form.bannerUrl = fileList[0].url
      this.uploads.show = false
    },
    handleChange(value) {
    },
    getDetail() {
      this.formLoading = true
      detail(this.id).then(res => {
        getForm(res, this.form);
      }).finally(() => {
        this.formLoading = false;
      })
    },
    handlerSubmit() {
      if (this.saveLoading || this.formLoading) {
        return;
      }
      this.$refs.form.validate(valid => {
        if (!valid) return;
        this.saveLoading = true;
        save(this.form).then(() => {
          this.$message.success('保存成功');
          this.close();
        }).finally(() => {
          this.saveLoading = false
        })
      })
    },
    close() {
      this.$emit('hideEditDialog')
    }
  }
}
</script>
<style lang="scss" scoped>
.img {
  width: 120px;
  height: 120px;
  margin-right: 10px;
}

.imgList {
  display: flex;
  flex-wrap: wrap;

  .img {
    width: 120px;
    height: 120px;
    margin-right: 10px;
  }
}
</style>
